#!/bin/bash


# --------------------------------------------------
# 用户界面显示选项定义
# --------------------------------------------------
option=(
  [1]="开始唤醒"       # 选项1显示名称
  [2]="自动公招"       # 选项2显示名称
  [3]="基建换班"       # 选项3显示名称 
  [4]="刷理智"         # 选项4显示名称
  [5]="获取信用及购物"  # 选项5显示名称
  [6]="领取奖励"       # 选项6显示名称
  [7]="自动肉鸽"       # 选项7显示名称
  [8]="生息演算 *"     # 选项8显示名称
)

# --------------------------------------------------
# 实际执行命令定义
# --------------------------------------------------
commands=(
  [1]="maa startup Official"               # 启动官方客户端
  [2]="maa run recruit.json"               # 自动公开招募
  [3]="maa run infrast.json"               # 基建换班操作
  [4]="maa run fight.json"                 # 刷理智
  [5]="maa run mall.json"                  # 收集信用及购物
  [6]="maa run award.json"                 # 领取奖励
  [7]="maa run roguelike.json"             # 肉鸽模式
  [8]=" "                                  # 生息演算
)

echo -e "${MAGENTA}"
cat << "EOF"
███████╗███████╗██╗  ██╗███████╗
██╔════╝██╔════╝██║ ██╔╝██╔════╝
█████╗  █████╗  █████╔╝ ███████╗
██╔══╝  ██╔══╝  ██╔═██╗ ╚════██║
██║     ███████╗██║  ██╗███████║
╚═╝     ╚══════╝╚═╝  ╚═╝╚══════╝
EOF
echo -e "${NC}"

# --------------------------------------------------
# ADB 初始设置模块
# --------------------------------------------------
read -p "是否使用ADB更改分辨率？[Y/n] (默认Y) " -n 1 change_res
echo
change_res=${change_res^^}

if [[ -z "$change_res" || "$change_res" == "Y" ]]; then
    read -p "请输入无线调试端口号（默认5555）： " port
    port=${port:-5555}
    
    echo -e "\n[ADB] 正在连接设备（端口：$port）..."
    if adb connect 127.0.0.1:$port ; then
        echo "[ADB] 正在设置分辨率..."
        adb shell wm size 1080x1920
       
        adb_set=true  # 标记需要恢复分辨率
    else
        echo "[警告] ADB连接失败！"
        adb_set=false
    fi
else
    adb_set=false
    echo "跳过ADB分辨率设置"
fi

# --------------------------------------------------
# 用户交互界面
# --------------------------------------------------
echo -e "\n可执行的命令列表：（带*表无法选择）"
for i in {1..8}; do
  echo "$i. ${option[$i]}"
done

echo -e "\n请输入要执行的命令编号（用空格分隔多个数字，按回车执行）："
read -r input

# --------------------------------------------------
# 游戏关闭确认模块
# --------------------------------------------------
read -p "执行完毕是否关闭游戏？[Y/n] (默认Y) " -n 1 should_close
echo
should_close=${should_close^^}
if [[ -z "$should_close" || "$should_close" == "Y" ]]; then
  close_command="maa closedown Official"
  echo "[提示] 将在执行完毕后自动关闭游戏"
else
  close_command=""
  echo "[提示] 执行完毕后将保持游戏运行"
fi

# --------------------------------------------------
# 命令执行模块
# --------------------------------------------------
for num in $input; do
  if [[ -n "${commands[$num]}" ]]; then
    echo -e "\n[任务$num] 正在执行：${option[$num]}"
    eval "${commands[$num]}"
  else
    echo "忽略无效编号：$num"
  fi
done

# --------------------------------------------------
# ADB 恢复设置模块
# --------------------------------------------------
if [ "$adb_set" = true ]; then
    echo -e "\n[ADB] 正在恢复原始分辨率..."
    adb shell wm size reset
    adb shell wm density reset
    echo "[ADB] 已恢复显示设置"
fi

# --------------------------------------------------
# 游戏关闭执行模块
# --------------------------------------------------
if [[ -n "$close_command" ]]; then
  echo -e "\n[关闭游戏] 执行中..."
  if eval "$close_command"; then
    echo "[完成] 游戏已关闭"
  else
    echo "[警告] 关闭游戏失败！"
  fi
fi

# --------------------------------------------------
# 脚本结束提示
# --------------------------------------------------
echo -e "\n所有任务执行完成"

# ==================================================
#                  _ooOoo_                         #
#                 o8888888o                        #
#                 88" . "88                        #
#                 (| -_- |)                        #
#                 O\  =  /O                        #
#              ____/`---'\____                     #
#            .'  \\|     |//  `.                   #
#           /  \\|||  :  |||//  \                  #
#          /  _||||| -:- |||||-  \                 #
#          |   | \\\  -  /// |   |                 #
#          | \_|  ''\---/''  |   |                 #
#          \  .-\__  `-`  ___/-. /                 #
#        ___`. .'  /--.--\  `. . __                #
#     ."" '<  `.___\_<|>_/___.'  >'"".             #
#    | | :  `- \`.;`\ _ /`;.`/ - ` : | |           #
#    \  \ `-.   \_ __\ /__ _/   .-` /  /           #
# =====`-.____`-.___\_____/___.-`____.-'======     #
#                   `=---='                        #
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #
#           佛祖保佑       永无BUG                  #
# ==================================================